package com.example.movie.data.movie.model

import android.os.Handler
import android.os.Looper
import android.os.Message
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import cn.forward.androids.utils.ThreadUtil
import com.example.movie.R
import com.example.movie.common.constants.LoadingState
import com.example.movie.common.model.RequestResult
import com.example.movie.common.model.Result
import com.example.movie.data.movie.model.vo.MovieListBeanVo
import com.example.movie.data.movie.model.vo.MovieListVo
import com.example.movie.data.movie.model.vo.MovieVo

class HomeViewModel(val repository: MovieRepository) : ViewModel() {

    private val _hotMovieData = MutableLiveData<RequestResult<List<MovieListBeanVo>>>()
    val hotMovieDataVo: LiveData<RequestResult<List<MovieListBeanVo>>>
        get() = _hotMovieData

    private val _comingSoonMovieData = MutableLiveData<RequestResult<List<MovieListBeanVo>>>()
    val comingSoonMovieDataVo: MutableLiveData<RequestResult<List<MovieListBeanVo>>>
        get() = _comingSoonMovieData

    private val _topList = MutableLiveData<List<MovieVo>>()
    val topList:MutableLiveData<List<MovieVo>>
        get() = _topList

    fun loadHomeMovieData() {
        var result: Result<MovieListVo>? = null;
        val handler = object : Handler(Looper.myLooper()!!) {
            override fun handleMessage(msg: Message) {
                if (msg.what == LoadingState.OK) {
                    if (result is Result.Success) {
                        _hotMovieData.value = RequestResult(
                            success = (result as Result.Success<MovieListVo>).data?.hotMovies
                        )
                        _comingSoonMovieData.value = RequestResult(
                            success = (result as Result.Success<MovieListVo>).data?.comingSoon
                        )
                    } else {
                        _hotMovieData.value = RequestResult(
                            error = R.string.request_fail
                        )
                        _comingSoonMovieData.value = RequestResult(
                            error = R.string.request_fail
                        )
                    }
                }
            }
        }

        ThreadUtil.getInstance().execute {
            result = repository.getMovieData()
            val message = Message()
            message.what = LoadingState.OK
            handler.sendMessage(message)
        }

    }

    fun loadMovieTopList() {
        var result: Result<List<MovieVo>>? = null;
        val handler = object : Handler(Looper.myLooper()!!) {
            override fun handleMessage(msg: Message) {
                if (msg.what == LoadingState.OK) {
                    _topList.value = (result as Result.Success<List<MovieVo>>).data
                }
            }
        }

        ThreadUtil.getInstance().execute {
            result = repository.loadMovieTopList()
            val message = Message()
            message.what = LoadingState.OK
            handler.sendMessage(message)
        }
    }

}